<?php

function getFromImgYa($key,$cpu) //Взять текст из гугла
{
	global $MySql,$outputDir,$imgDir;
	$maska="http://images.yandex.ru/yandsearch?text=";
	$myURL=$maska.str_replace(" ","+",$key);
	$ch = curl_init ();
	curl_setopt ( $ch , CURLOPT_URL , $myURL);
	curl_setopt ( $ch , CURLOPT_HEADER , 0 );
	curl_setopt($ch, CURLOPT_FAILONERROR, 1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
	$itog=curl_exec ( $ch );
	curl_close ( $ch );
	//onmousedown="ic(11)"><img src="http://im7-tub.yandex.net/i?id=78309384&amp;tov=7" width="96" height="142" alt=""/>
	preg_match('/http:\/\/(.*?)?-tub.yandex.net\/i\?id=(.*?)?&amp;tov=([\d]+?)?"/i',$itog,$id);
	$link="http://{$id[1]}-tub.yandex.net/i?id={$id[2]}&tov={$id[3]}";
 	$ch = curl_init ();
	curl_setopt ( $ch , CURLOPT_URL , $link);
	curl_setopt ( $ch , CURLOPT_HEADER , 0 );
	curl_setopt($ch, CURLOPT_FAILONERROR, 1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
	$itog=curl_exec ( $ch );
	curl_close ( $ch ); 

	$wFile=fopen("../".$imgDir."/".$cpu.".jpg",'w');
	fwrite($wFile,$itog);
	fclose($wFile);

    return "<img src='/{$imgDir}/{$cpu}.jpg'>"; 
}
?>
